<template>
  <div class="articles">
    <!-- 上部的表单 -->
    <el-card class="box-card">
      <template v-slot:header>
        <my-breadcrumb>首页管理</my-breadcrumb>
      </template>

      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="form.channel_id" placeholder="请选择频道">
            <el-option
              v-for="item in channels"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-col :span="11">
            <el-date-picker
              v-model="form.date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button @click="search" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 下部表格 -->
    <el-card>
      <template>
        <div>
          根据筛选条件共查询到{{ total_count }}条数据,当前是{{ page }}页
        </div>
      </template>

      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        :data="articles"
        style="width: 100%"
      >
        <el-table-column label="封面">
          <template v-slot:default="obj">
            <el-image
              style="width: 150px; height: 100px"
              :src="obj.row.cover.images[0]"
              fit="fit"
            >
              <div slot="error" class="image-slot">
                <img
                  src="@/assets/error.gif"
                  alt=""
                  style="width: 150px; height: 100px"
                />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template v-slot:default="obj">
            <el-tag type="success" v-if="obj.row.status === 2">审核通过</el-tag>
            <el-tag type="warning" v-if="obj.row.status === 0">草稿</el-tag>
            <el-tag type="info" v-if="obj.row.status === 1">待审核</el-tag>
            <el-tag type="danger" v-if="obj.row.status === 3">审核失败</el-tag>
            <el-tag v-if="obj.row.status === 4">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="obj">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="edit(obj.row.id)"
              circle
            ></el-button>
            <el-button
              @click="del(obj.row.id)"
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="changePage"
      :total="total_count"
      :current-page="page"
      :page-size="10"
    >
    </el-pagination>
  </div>
</template>

<script>
import { reqGetArticles, reqGetChannels, reqDeleteArticle } from '@/api/articles'
import MyBreadcrumb from '@/components/my-breadcrumb.vue'
export default {
  name: 'Articles',
  data () {
    return {
      form: {
        status: null,
        channel_id: null,
        date: []
      },
      articles: [],
      page: 1, // 当前页
      total_count: 0, // 总条数
      channels: [],
      loading: false,
      searchObj: {
        status: null,
        channel_id: null,
        date: []
      }
    }
  },
  components: {
    MyBreadcrumb
  },
  created () {
    this.getArticles()
    this.getChannels()
  },
  methods: {
    del (id) {
      this.$confirm('是否确认删除', '提示', {
        type: 'warning'
      }).then(() => {
        reqDeleteArticle(id).then(res => {
          this.$message.success('已删除')
        }).catch((err) => {
          this.$message.error('目前是假数据，无法删除')
          console.log(err)
        }).catch(() => {
          console.log('已删除')
        })
      })
    },
    getArticles () {
      this.loading = true
      const [beginPubdate, endPubdate] = this.searchObj.date
      reqGetArticles({
        page: this.page,
        begin_pubdate: beginPubdate,
        end_pubdate: endPubdate,
        status: this.searchObj.status,
        channel_id: this.searchObj.channel_id
      }).then((res) => {
        // console.log(res)
        // console.log(this.form.date)
        this.articles = res.data.data.results
        this.total_count = res.data.data.total_count
        this.loading = false
      })
    },
    getChannels () {
      reqGetChannels().then((res) => {
        // console.log(res.data)
        this.channels = res.data.data.channels
        console.log(this.channels)
      })
    },
    search () {
      this.searchObj.status = this.form.status
      this.searchObj.channel_id = this.form.channel_id
      this.searchObj.date = this.form.date
      this.page = 1
      this.getArticles()
    },
    changePage (index) {
      // console.log(index)
      this.page = index
      this.getArticles()
    },
    edit (id) {
      const articlesId = id.toString()
      this.$router.push(`/edit-articles/${articlesId}`)
    }
  }
}
</script>

<style lang='less' scoped>
.box-card {
  margin-bottom: 20px;
}
.el-pagination {
  margin-top: 20px;
}
</style>
